package cn.big.bigd.biocode.service;

import org.springframework.stereotype.Service;


/**
 * IP地址服务
 */
@Service
public class IpAddressService {

/*    private static Logger logger = LoggerFactory.getLogger(IpAddressService.class);

//    private static String dbPath = "/Users/wangfan/IdeaProjects/bigd-biocode/src/main/webapp/static/asserts/GeoLite2-Country.mmdb";
    private static String dbPath = "/Users/wangfan/IdeaProjects/bigd-biocode/src/main/webapp/static/asserts/GeoIP.dat";

    private static DatabaseReader reader;

    @Autowired
    private Environment env;

    @PostConstruct
    public void init() {
        try {
            String path = env.getProperty("geolite2.country.db.path");
            if (StringUtils.isNotBlank(path)) {
                dbPath = path;
            }
            File database = new File(dbPath);
            reader = new DatabaseReader.Builder(database).build();
        } catch (Exception e) {
            logger.error("IP地址服务初始化异常:" + e.getMessage(), e);
        }
    }


    public String getSubdivision(String ipAddress) {
        try {
            CityResponse response = reader.city(InetAddress.getByName(ipAddress));
            return response.getMostSpecificSubdivision().getNames().get("zh-CN");
        } catch (Exception e) {
            logger.error("根据IP[{}]获取省份失败:{}", ipAddress, e.getMessage());
            return null;
        }
    }

    public String getCountry(String ipAddress) {
        try {
            CountryResponse response = reader.country(InetAddress.getByName(ipAddress));
            return response.getCountry().getIsoCode();
        } catch (Exception e) {
            logger.error("根据IP[{}]获取代码失败:{}", ipAddress, e.getMessage());
            return null;
        }
    }*/
}